//
//  RHAgencyApplyCell.m
//  RuiHeAppApp
//
//  Created by Yan Chen on 2023/7/14.
//

#import "RHAgencyApplyCell.h"

@interface RHAgencyApplyCell()<UITextFieldDelegate>

@property (nonatomic, strong) UIImageView   *arrowImgView;

@property (nonatomic, strong) QMUILabel     *titleLab;

@property (nonatomic ,strong) QMUITextField   *textField;

@property(nonatomic  ,strong)UIView *cornerView;

@property(nonatomic,strong)RHApplyBaseInfoModel *infoModel;

@property(nonatomic ,copy)NSString *key;

@property(nonatomic ,strong)NSMutableDictionary *para;
@end

@implementation RHAgencyApplyCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if(self){
        [self setupUI];
    }
    return self;
}

-(void)hideTextfieldKeyboard
{
    [self.textField resignFirstResponder];
}

- (void)setupUI{
    
    self.contentView.backgroundColor = UIColor.whiteColor;
    _cornerView = kView;
    [self.contentView addSubview:_cornerView];
    _cornerView.backgroundColor = [UIColor whiteColor];
    _cornerView.layer.cornerRadius = 8;
    _cornerView.layer.maskedCorners = kCALayerMinXMinYCorner|kCALayerMaxXMinYCorner;
    _cornerView.layer.masksToBounds = YES;
    [_cornerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.insets(UIEdgeInsetsZero);
    }];
    
    _arrowImgView = [[UIImageView alloc] init];
    [_arrowImgView setBackgroundColor:UIColor.whiteColor];
    _arrowImgView.image = kImage(@"blackRightArrow");
    [self.contentView addSubview:_arrowImgView];
    
    kWeakSelf;
    [_arrowImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-15);
        make.centerY.equalTo(weakSelf.contentView);
        make.size.mas_equalTo(CGSizeMake(_arrowImgView.image.size.width, _arrowImgView.image.size.height));
    }];
    
    _titleLab = [[QMUILabel alloc]init];
    _titleLab.text = @"National agency";
    _titleLab.font = kSYSTEMFONT(15);
    _titleLab.numberOfLines = 0;
    _titleLab.textColor = kColorRGB(53, 53, 53);
    _titleLab.textAlignment = NSTextAlignmentLeft;
    [self.contentView addSubview:_titleLab];
    
    [_titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.centerY.equalTo(self.contentView);
     }];
    
    
    _textField = [[QMUITextField alloc] init];
    [_textField setFont:kSYSTEMFONT(12)];
    _textField.placeholder = @"please enter";
    _textField.textColor = kColorRGB(144, 144, 144);
    _textField.textAlignment = NSTextAlignmentRight;
    [_textField setAutocapitalizationType:UITextAutocapitalizationTypeNone];
    [_textField setAutocorrectionType:UITextAutocorrectionTypeNo];
    _textField.delegate = self;
    [self.contentView addSubview:_textField];
    
    [_textField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(_arrowImgView.mas_left).offset(-8);
        make.left.equalTo(_titleLab.mas_right).offset(10);
        make.centerY.equalTo(self.contentView);
        make.height.offset(40);
    }];
    
    UILabel *line = kLab;
    line.backgroundColor = kColorRGB(235, 235, 235);
    [self.contentView addSubview:line];
    
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.right.mas_equalTo(-15);
        make.height.mas_equalTo(1.0);
        make.bottom.mas_equalTo(0);
     }];

    
    [[RACSignal merge:@[_textField.rac_textSignal,RACObserve(_textField, text)]]subscribeNext:^(NSString   * _Nullable text) {
        weakSelf.infoModel.content = text;
        if(weakSelf.infoModel.index != 0){
            [weakSelf.para setObject:text forKey:weakSelf.key];
        }
        NSLog(@"----->参数%ld text:%@  %@ ",weakSelf.infoModel.index,text,weakSelf.para);
    }];
    
   
    
}
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
    [[RHWindowTipManage shareWindowTipManage] hidden];
}

-(void)setInfoModel:(RHApplyBaseInfoModel*)infoModel para:(NSMutableDictionary*)para paraKey:(NSString*)key;
{
    _para = para;
    _key = key;
    _infoModel = infoModel;
    
    if(infoModel.content.length==0){
        _textField.placeholder = infoModel.placeHold;
    }
    self.textField.keyboardType = UIKeyboardTypeDefault;
    if(_infoModel.index == 2 || _infoModel.index == 3 || _infoModel.index == 7){
        _textField.userInteractionEnabled = NO;
    }else{
        _textField.userInteractionEnabled = YES;
    }
    
    if(_infoModel.index == 5){//电话
        self.textField.keyboardType = UIKeyboardTypeNumberPad;
    }
    if(_infoModel.index == 8){//公司网站
        self.textField.keyboardType = UIKeyboardTypeURL;
    }
    
    
    //公司全称 8 - 40个字符
    if(_infoModel.index == 0 || _infoModel.index == 1){
        _textField.maximumTextLength = 40;
    }
    
    if(_infoModel.index == 4){//公司法人 2 - 32个字符
        _textField.maximumTextLength = 32;
    }
    if(_infoModel.index == 5){//电话 11 位数
        _textField.maximumTextLength = 11;
    }
    
    if(_infoModel.index == 6||_infoModel.index == 8){//公司地址,网址 8-64 字符
        _textField.maximumTextLength = 64;
    }
    
    if(_infoModel.index == 9){//经营范围 16-200 字符
        _textField.maximumTextLength = 200;
    }
    
    _titleLab.text = infoModel.title;
    _textField.text = infoModel.content;
    
    self.cornerView.hidden = _infoModel.index != 0;
    self.contentView.backgroundColor = _infoModel.index == 0? CFTabViewBgColor:[UIColor whiteColor];
}



@end
